<?php


namespace app\admin\controller;
use think\facade\View;
use app\BaseController;
class Login extends BaseController
{
    /**
     * @return \think\response\View
     * 后台登录页
     */
    public function login()
    {
        if(session(config("session.session_admin"))){
            return redirect(url('Index/index'));
        }else{
            return view();
        }
    }

    /**
     * @return \think\response\Json
     * 后台登录判断
     */
    public function judge()
    {
        //是否为post请求访问
        if(!$this->request->isPost()){
            return show(config("status.error"),'请求方式错误');
        }

        //接收用户名与密码并进行合法校验
        $mes = $this->request->param('mes');
        $data = [
            'username' => $mes['username'],
            'password' => $mes['password']
        ];
        $validate = new \app\admin\validate\AdminUser();
        if(!$validate->check($data)){
            return show (config("status.error"),$validate->getError());
        }

        //调用business层的AdminUser类中login方法
        try{
            $result = \app\admin\business\AdminUser::login($data);
        }catch (\Exception $e){
            return show (config("status.error"),$e->getMessage());
        }

       if($result){
           return show(config("status.success"),'登录成功');
       }else{
           return show(config("status.error"),'登录失败');
       }
    }

    public function logout()
    {
        session(config("session.session_admin"),null);
        return redirect(url('Login/login'));
    }
}